package com.web.cms.pan.provider;

import com.blue.core.dict.State;
import com.blue.core.dict.UploadType;
import com.blue.integration.serve.StorageClient;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.GetModel;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.blue.validation.spi.IdListValidator;
import com.web.cms.pan.exception.FileDataException;
import com.web.cms.pan.facade.FileDataFacade;
import com.web.cms.pan.message.FileDataRequest;
import com.web.cms.pan.message.FileDataResponse;
import com.web.cms.pan.message.FolderFileRequest;
import com.web.cms.pan.message.FolderFileResponse;
import com.web.cms.pan.model.FileData;
import com.web.cms.pan.model.FolderFile;
import com.web.cms.pan.service.FileDataService;
import com.web.cms.pan.util.FileDataConvertor;
import com.web.cms.pan.util.FolderFileConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文件数据接口实现类
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年04月09日
 */
@Component("panFileDataProvider")
public class FileDataProvider implements FileDataFacade
{
	@Autowired
	private FileDataService fileDataService;
	@Autowired
	private StorageClient storageClient;

	@Override
	public void add(FileDataRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		FileData fileData = FileDataConvertor.toFileData(request);
		fileData.setType(UploadType.getType(fileData.getMimeType(), fileData.getName()));
		fileDataService.add(fileData, request.getUserId(), request.getFolderId());
	}

	@Override
	public void update(FolderFileRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		FolderFile folderFile = FolderFileConvertor.toFolderFile(request);
		fileDataService.update(folderFile);
	}

	@Override
	public void state(FolderFileRequest request)
	{
		ValidationUtil.valid(request, GetModel.class);
		FolderFile folderFile = FolderFileConvertor.toFolderFile(request);
		fileDataService.state(folderFile);
	}

	@Override
	public List<FolderFileResponse> list(Integer userId, Integer folderId, State state)
	{
		if (userId == null)
			throw new FileDataException("用户ID不能为空");
		if (folderId == null)
			throw new FileDataException("目录ID不能为空");

		FolderFile folderFile = new FolderFile();
		folderFile.setUserId(userId);
		folderFile.setFolderId(folderId);
		folderFile.setState(state);
		List<FolderFile> list = fileDataService.listFolderFile(folderFile);
		List<FolderFileResponse> responseList = FolderFileConvertor.toResponse(list, storageClient);
		return responseList;
	}

	@Override
	public void operate(Integer userId, int folderFileId, int folderId, int type)
	{
		if (userId == null)
			throw new FileDataException("用户ID不能为空");
		if (type == 0)
			throw new FileDataException("未知操作");

		if (folderId == -1)
			folderId = 0;

		if (type == 1) // 移动
		{
			fileDataService.move(userId, folderFileId, folderId);
		}
		else if (type == 2) // 复制
		{
			fileDataService.copy(userId, folderFileId, folderId);
		}
	}

	@Override
	public void delete(Integer userId, String ids)
	{
		if (userId == null)
			throw new FileDataException("用户ID不能为空");
		if (ids == null || ids.isEmpty() || !IdListValidator.isValid(ids))
			throw new FileDataException("文件ID列表错误");

		List<Integer> idList = Arrays.stream(ids.split(",")).map(Integer::valueOf).collect(Collectors.toList());
		fileDataService.delete(userId, idList);
	}

	@Override
	public Page<FileDataResponse> listFilePage(FileDataRequest request, int start, int size)
	{
		FileData fileData = FileDataConvertor.toFileData(request);
		Page<FileData> page = new Page<>(size);
		page.setCurPage(start);
		page = fileDataService.list(fileData, page);
		Page<FileDataResponse> responsePage = FileDataConvertor.toResponse(page, storageClient);
		return responsePage;
	}
}
